#include "ChoiceDTD.h"

using namespace std;


string ChoiceDTD::regExpCreate() const
{
    string regExp = "(";

    /* For each component of the structure, we generate its regular expression and append it to regExp */
    vector<ComponentDTD*>::const_iterator it;
    for( it = components.begin() ; it != components.end() ; ++it)
    {
        regExp += (*it)->regExpCreate();

        it++;
        if( it != components.end() ) // just to avoid putting the separating caracter at the end of the choice
        {
            regExp += "|" ; // in a choice, the components are separated by a pipe
        }
        it--;
    }

    regExp += ")";

    /* adding the cardinality */
    if(card == ONE_N)
        regExp += "+";
    else if(card == ZERO_N)
        regExp += "*";
    else if(card == ZERO_ONE)
        regExp += "?";

    return regExp;
}

void ChoiceDTD::display() const
{
    printf("(");

    std::vector<ComponentDTD *>::const_iterator it;
    for( it = components.begin() ; it != components.end() ; ++it)
    {
        (*it)->display();
        it++;

        if( it != components.end() ) // just to avoid putting the separating caracter at the end of the choice
        {
            printf(" | "); // in a choice, the components are separated by a pipe
        }
        it--;
    }

    printf(")");

    // displaying cardinality
    if(card == ONE_N)
        printf("+");
    else if(card == ZERO_N)
        printf("*");
    else if(card == ZERO_ONE)
        printf("?");
}
